<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Reducer 重构示例 | Redux 中文文档 Join the chat at https://gitter.im/camsong/redux-in-chinese</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.3.3">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../../gitbook/plugins/gitbook-plugin-edit-link/plugin.css">
        
    
        
        <link rel="stylesheet" href="../../../gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
    

        
    
    
    <link rel="next" href="../../../docs/recipes/reducers/UsingCombineReducers.html" />
    
    
    <link rel="prev" href="../../../docs/recipes/reducers/SplittingReducerLogic.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="4.9.4" data-basepath="../../.." data-revision="Thu Mar 16 2017 23:41:56 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        自述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="docs/introduction/index.html">
            
                
                    <a href="../../../docs/introduction/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        介绍
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="docs/introduction/Motivation.html">
            
                
                    <a href="../../../docs/introduction/Motivation.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        动机
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="docs/introduction/CoreConcepts.html">
            
                
                    <a href="../../../docs/introduction/CoreConcepts.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        核心概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="docs/introduction/ThreePrinciples.html">
            
                
                    <a href="../../../docs/introduction/ThreePrinciples.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        三大原则
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="docs/introduction/PriorArt.html">
            
                
                    <a href="../../../docs/introduction/PriorArt.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        先前技术
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="docs/introduction/Ecosystem.html">
            
                
                    <a href="../../../docs/introduction/Ecosystem.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        生态系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="docs/introduction/Examples.html">
            
                
                    <a href="../../../docs/introduction/Examples.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        示例
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="docs/basics/index.html">
            
                
                    <a href="../../../docs/basics/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="docs/basics/Actions.html">
            
                
                    <a href="../../../docs/basics/Actions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="docs/basics/Reducers.html">
            
                
                    <a href="../../../docs/basics/Reducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        Reducer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="docs/basics/Store.html">
            
                
                    <a href="../../../docs/basics/Store.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="docs/basics/DataFlow.html">
            
                
                    <a href="../../../docs/basics/DataFlow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        数据流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="docs/basics/UsageWithReact.html">
            
                
                    <a href="../../../docs/basics/UsageWithReact.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        搭配 React
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="docs/basics/ExampleTodoList.html">
            
                
                    <a href="../../../docs/basics/ExampleTodoList.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        示例：Todo List
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="docs/advanced/index.html">
            
                
                    <a href="../../../docs/advanced/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="docs/advanced/AsyncActions.html">
            
                
                    <a href="../../../docs/advanced/AsyncActions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        异步 Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="docs/advanced/AsyncFlow.html">
            
                
                    <a href="../../../docs/advanced/AsyncFlow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        异步数据流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="docs/advanced/Middleware.html">
            
                
                    <a href="../../../docs/advanced/Middleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Middleware
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="docs/advanced/UsageWithReactRouter.html">
            
                
                    <a href="../../../docs/advanced/UsageWithReactRouter.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        搭配 React Router
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="docs/advanced/ExampleRedditAPI.html">
            
                
                    <a href="../../../docs/advanced/ExampleRedditAPI.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        示例：Reddit API
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="docs/advanced/NextSteps.html">
            
                
                    <a href="../../../docs/advanced/NextSteps.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        下一步
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="docs/recipes/index.html">
            
                
                    <a href="../../../docs/recipes/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        技巧
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="docs/recipes/MigratingToRedux.html">
            
                
                    <a href="../../../docs/recipes/MigratingToRedux.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        迁移到 Redux
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="docs/recipes/UsingObjectSpreadOperator.html">
            
                
                    <a href="../../../docs/recipes/UsingObjectSpreadOperator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        使用对象展开运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="docs/recipes/ReducingBoilerplate.html">
            
                
                    <a href="../../../docs/recipes/ReducingBoilerplate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        减少样板代码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="docs/recipes/ServerRendering.html">
            
                
                    <a href="../../../docs/recipes/ServerRendering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        服务端渲染
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="docs/recipes/WritingTests.html">
            
                
                    <a href="../../../docs/recipes/WritingTests.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        编写测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="docs/recipes/ComputingDerivedData.html">
            
                
                    <a href="../../../docs/recipes/ComputingDerivedData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        计算衍生数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="docs/recipes/ImplementingUndoHistory.html">
            
                
                    <a href="../../../docs/recipes/ImplementingUndoHistory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        实现撤销重做
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="docs/recipes/IsolatingSubapps.html">
            
                
                    <a href="../../../docs/recipes/IsolatingSubapps.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        子应用隔离
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="docs/recipes/StructuringReducers.html">
            
                
                    <a href="../../../docs/recipes/StructuringReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.</b>
                        
                        组织 Reducer
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.9.1" data-path="docs/recipes/reducers/PrerequisiteConcepts.html">
            
                
                    <a href="../../../docs/recipes/reducers/PrerequisiteConcepts.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.1.</b>
                        
                        Reducer 基础概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.2" data-path="docs/recipes/reducers/BasicReducerStructure.html">
            
                
                    <a href="../../../docs/recipes/reducers/BasicReducerStructure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.2.</b>
                        
                        Reducer 基础结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.3" data-path="docs/recipes/reducers/SplittingReducerLogic.html">
            
                
                    <a href="../../../docs/recipes/reducers/SplittingReducerLogic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.3.</b>
                        
                        Reducer 逻辑拆分
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4.9.4" data-path="docs/recipes/reducers/RefactoringReducersExample.html">
            
                
                    <a href="../../../docs/recipes/reducers/RefactoringReducersExample.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.4.</b>
                        
                        Reducer 重构示例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.5" data-path="docs/recipes/reducers/UsingCombineReducers.html">
            
                
                    <a href="../../../docs/recipes/reducers/UsingCombineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.5.</b>
                        
                        `combineReducers` 用法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.6" data-path="docs/recipes/reducers/BeyondCombineReducers.html">
            
                
                    <a href="../../../docs/recipes/reducers/BeyondCombineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.6.</b>
                        
                        `combineReducers` 进阶
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.7" data-path="docs/recipes/reducers/NormalizingStateShape.html">
            
                
                    <a href="../../../docs/recipes/reducers/NormalizingStateShape.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.7.</b>
                        
                        State 范式化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.8" data-path="docs/recipes/reducers/UpdatingNormalizedData.html">
            
                
                    <a href="../../../docs/recipes/reducers/UpdatingNormalizedData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.8.</b>
                        
                        管理范式化数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.9" data-path="docs/recipes/reducers/ReusingReducerLogic.html">
            
                
                    <a href="../../../docs/recipes/reducers/ReusingReducerLogic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.9.</b>
                        
                        Reducer 逻辑复用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.10" data-path="docs/recipes/reducers/ImmutableUpdatePatterns.html">
            
                
                    <a href="../../../docs/recipes/reducers/ImmutableUpdatePatterns.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.10.</b>
                        
                        不可变更新模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.11" data-path="docs/recipes/reducers/InitializingState.html">
            
                
                    <a href="../../../docs/recipes/reducers/InitializingState.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.11.</b>
                        
                        初始化 State
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="docs/FAQ.html">
            
                
                    <a href="../../../docs/FAQ.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        常见问题
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="docs/faq/General.html">
            
                
                    <a href="../../../docs/faq/General.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        综合
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="docs/faq/Reducers.html">
            
                
                    <a href="../../../docs/faq/Reducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        Reducer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="docs/faq/OrganizingState.html">
            
                
                    <a href="../../../docs/faq/OrganizingState.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        组织 State
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="docs/faq/StoreSetup.html">
            
                
                    <a href="../../../docs/faq/StoreSetup.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        创建 Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="docs/faq/Actions.html">
            
                
                    <a href="../../../docs/faq/Actions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="docs/faq/CodeStructure.html">
            
                
                    <a href="../../../docs/faq/CodeStructure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        代码结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="docs/faq/Performance.html">
            
                
                    <a href="../../../docs/faq/Performance.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        性能
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="docs/faq/ReactRedux.html">
            
                
                    <a href="../../../docs/faq/ReactRedux.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        React Redux
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.9" data-path="docs/faq/Miscellaneous.html">
            
                
                    <a href="../../../docs/faq/Miscellaneous.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                        其它
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="docs/Troubleshooting.html">
            
                
                    <a href="../../../docs/Troubleshooting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        排错
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="docs/Glossary.html">
            
                
                    <a href="../../../docs/Glossary.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        词汇表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="docs/api/index.html">
            
                
                    <a href="../../../docs/api/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        API 文档
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="docs/api/createStore.html">
            
                
                    <a href="../../../docs/api/createStore.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        createStore
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="docs/api/Store.html">
            
                
                    <a href="../../../docs/api/Store.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="docs/api/combineReducers.html">
            
                
                    <a href="../../../docs/api/combineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        combineReducers
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="docs/api/applyMiddleware.html">
            
                
                    <a href="../../../docs/api/applyMiddleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        applyMiddleware
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="docs/api/bindActionCreators.html">
            
                
                    <a href="../../../docs/api/bindActionCreators.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        bindActionCreators
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="docs/api/compose.html">
            
                
                    <a href="../../../docs/api/compose.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        compose
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="docs/react-redux/index.html">
            
                
                    <a href="../../../docs/react-redux/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        react-redux 文档
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="docs/react-redux/api.html">
            
                
                    <a href="../../../docs/react-redux/api.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        API
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="docs/react-redux/troubleshooting.html">
            
                
                    <a href="../../../docs/react-redux/troubleshooting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        排错
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" >
            
                
                    <a target="_blank" href="https:/github.com/react-guide/redux-tutorial-cn">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        redux-tutorial
                    </a>
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../../" >Redux 中文文档 Join the chat at https://gitter.im/camsong/redux-in-chinese</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <a id="edit-link" href="https://github.com/camsong/redux-in-chinese/tree/master/docs/recipes/reducers/RefactoringReducersExample.md" class="btn fa fa-edit pull-left">&#xA0;&#xA0;&#x5F00;&#x59CB;&#x7EA0;&#x9519;</a><h1 id="%E4%BD%BF%E7%94%A8%E5%87%BD%E6%95%B0%E5%88%86%E8%A7%A3%EF%BC%88functional-decomposition%EF%BC%89%E5%92%8C-reducer-%E7%BB%84%E5%90%88%EF%BC%88reducer-composition%EF%BC%89%E9%87%8D%E6%9E%84-reducer">&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x5206;&#x89E3;&#xFF08;Functional Decomposition&#xFF09;&#x548C; Reducer &#x7EC4;&#x5408;&#xFF08;Reducer Composition&#xFF09;&#x91CD;&#x6784; Reducer</h1>
<p>&#x770B;&#x770B;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x7684; <code>sub<span class="token operator">-</span>reducer</code> &#x548C;&#x5982;&#x4F55;&#x628A;&#x4ED6;&#x4EEC;&#x7EC4;&#x5408;&#x5728;&#x4E00;&#x8D77;&#x7684;&#x4F8B;&#x5B50;&#x662F;&#x5F88;&#x6709;&#x7528;&#x7684;&#x3002;&#x73B0;&#x5728;&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x770B;&#x5982;&#x4F55;&#x5C06;&#x4E00;&#x4E2A;&#x5927;&#x578B;&#x7684;&#x5355;&#x4E2A; reducer &#x91CD;&#x6784;&#x4E3A;&#x591A;&#x4E2A;&#x6BD4;&#x8F83;&#x5C0F;&#x7684;&#x51FD;&#x6570;&#x7684;&#x7EC4;&#x5408;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A; &#x4E3A;&#x4E86;&#x8BF4;&#x660E;&#x91CD;&#x6784;&#x7684;&#x6982;&#x5FF5;&#x548C;&#x8FC7;&#x7A0B;&#x800C;&#x4E0D;&#x662F;&#x4E3A;&#x4E86;&#x7F16;&#x5199;&#x7B80;&#x6D01;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x662F;&#x7279;&#x610F;&#x4EE5;&#x5197;&#x957F;&#x7684;&#x98CE;&#x683C;&#x7F16;&#x5199;&#x7684;</p>
</blockquote>
<h4 id="%E5%88%9D%E9%81%87-reducer">&#x521D;&#x9047; Reducer</h4>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x770B;&#x521D;&#x59CB; reducer &#x957F;&#x4EC0;&#x4E48;&#x6837;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="token keyword">const</span> initialState <span class="token operator">=</span> <span class="token punctuation">{</span>
    visibilityFilter <span class="token punctuation">:</span> <span class="token string">&apos;SHOW_ALL&apos;</span><span class="token punctuation">,</span>
    todos <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>


<span class="token keyword">function</span> <span class="token function">appReducer</span><span class="token punctuation">(</span>state <span class="token operator">=</span> initialState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">switch</span><span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token string">&apos;SET_VISIBILITY_FILTER&apos;</span> <span class="token punctuation">:</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> state<span class="token punctuation">,</span> <span class="token punctuation">{</span>
                visibilityFilter <span class="token punctuation">:</span> action<span class="token punctuation">.</span>filter
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">case</span> <span class="token string">&apos;ADD_TODO&apos;</span> <span class="token punctuation">:</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> state<span class="token punctuation">,</span> <span class="token punctuation">{</span>
                todos <span class="token punctuation">:</span> state<span class="token punctuation">.</span>todos<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
                    id<span class="token punctuation">:</span> action<span class="token punctuation">.</span>id<span class="token punctuation">,</span>
                    text<span class="token punctuation">:</span> action<span class="token punctuation">.</span>text<span class="token punctuation">,</span>
                    completed<span class="token punctuation">:</span> <span class="token keyword">false</span>
                <span class="token punctuation">}</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">case</span> <span class="token string">&apos;TOGGLE_TODO&apos;</span> <span class="token punctuation">:</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> state<span class="token punctuation">,</span> <span class="token punctuation">{</span>
                todos <span class="token punctuation">:</span> state<span class="token punctuation">.</span>todos<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>todo <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>todo<span class="token punctuation">.</span>id <span class="token operator">!</span><span class="token operator">==</span> action<span class="token punctuation">.</span>id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                      <span class="token keyword">return</span> todo<span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> todo<span class="token punctuation">,</span> <span class="token punctuation">{</span>
                        completed <span class="token punctuation">:</span> <span class="token operator">!</span>todo<span class="token punctuation">.</span>completed
                    <span class="token punctuation">}</span><span class="token punctuation">)</span>
                  <span class="token punctuation">}</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">case</span> <span class="token string">&apos;EDIT_TODO&apos;</span> <span class="token punctuation">:</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> state<span class="token punctuation">,</span> <span class="token punctuation">{</span>
                todos <span class="token punctuation">:</span> state<span class="token punctuation">.</span>todos<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>todo <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>todo<span class="token punctuation">.</span>id <span class="token operator">!</span><span class="token operator">==</span> action<span class="token punctuation">.</span>id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                      <span class="token keyword">return</span> todo<span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>

                    <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> todo<span class="token punctuation">,</span> <span class="token punctuation">{</span>
                        text <span class="token punctuation">:</span> action<span class="token punctuation">.</span>text
                    <span class="token punctuation">}</span><span class="token punctuation">)</span>
                  <span class="token punctuation">}</span><span class="token punctuation">)</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">default</span> <span class="token punctuation">:</span> <span class="token keyword">return</span> state<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x975E;&#x5E38;&#x77ED;&#xFF0C;&#x4F46;&#x5DF2;&#x7ECF;&#x5F00;&#x59CB;&#x53D8;&#x5F97;&#x6BD4;&#x8F83;&#x590D;&#x6742;&#x3002;&#x6211;&#x4EEC;&#x5728;&#x5904;&#x7406;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x533A;&#x57DF;&#xFF08;filtering &#x548C; todo &#x5217;&#x8868;&#xFF09;&#xFF0C;&#x5D4C;&#x5957;&#x4F7F;&#x5F97;&#x66F4;&#x65B0;&#x903B;&#x8F91;&#x96BE;&#x4EE5;&#x9605;&#x8BFB;&#xFF0C;&#x5E76;&#x4E14;&#x4F1A;&#x8BA9;&#x6211;&#x4EEC;&#x4E0D;&#x6E05;&#x695A;&#x5230;&#x5E95;&#x662F;&#x4EC0;&#x4E48;&#x8DDF;&#x4EC0;&#x4E48;&#x3002;</p>
<h4 id="%E6%8F%90%E5%8F%96%E5%B7%A5%E5%85%B7%E5%87%BD%E6%95%B0%EF%BC%88extracting-utility-functions%EF%BC%89">&#x63D0;&#x53D6;&#x5DE5;&#x5177;&#x51FD;&#x6570;&#xFF08;Extracting Utility Functions&#xFF09;</h4>
<p>&#x7B2C;&#x4E00;&#x6B65;&#x662F;&#x5199;&#x4E00;&#x4E2A;&#x8FD4;&#x56DE;&#x66F4;&#x65B0;&#x4E86;&#x76F8;&#x5E94;&#x533A;&#x57DF;&#x7684;&#x65B0;&#x5BF9;&#x8C61;&#x3002;&#x8FD9;&#x513F;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x91CD;&#x590D;&#x7684;&#x903B;&#x8F91;&#x662F;&#x5728;&#x66F4;&#x65B0;&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x7279;&#x5B9A;&#x9879;&#x76EE;&#xFF0C;&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x5C06;&#x4ED6;&#x63D0;&#x6210;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x3002;</p>
<pre><code class="lang-javascript"><span class="token keyword">function</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>oldObject<span class="token punctuation">,</span> newValues<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">// &#x7528;&#x7A7A;&#x5BF9;&#x8C61;&#x4F5C;&#x4E3A;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9; Object.assign&#xFF0C;&#x4EE5;&#x786E;&#x4FDD;&#x662F;&#x590D;&#x5236;&#x6570;&#x636E;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x53BB;&#x6539;&#x53D8;&#x539F;&#x6765;&#x7684;&#x6570;&#x636E;</span>
    <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> oldObject<span class="token punctuation">,</span> newValues<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">updateItemInArray</span><span class="token punctuation">(</span>array<span class="token punctuation">,</span> itemId<span class="token punctuation">,</span> updateItemCallback<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> updatedItems <span class="token operator">=</span> array<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>item <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>id <span class="token operator">!</span><span class="token operator">==</span> itemId<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">// &#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x53EA;&#x60F3;&#x66F4;&#x65B0;&#x4E00;&#x4E2A;&#x9879;&#x76EE;&#xFF0C;&#x6240;&#x4EE5;&#x4FDD;&#x7559;&#x6240;&#x6709;&#x7684;&#x5176;&#x4ED6;&#x9879;&#x76EE;</span>
            <span class="token keyword">return</span> item<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment" spellcheck="true">// &#x4F7F;&#x7528;&#x63D0;&#x4F9B;&#x7684;&#x56DE;&#x8C03;&#x6765;&#x521B;&#x5EFA;&#x65B0;&#x7684;&#x9879;&#x76EE;</span>
        <span class="token keyword">const</span> updatedItem <span class="token operator">=</span> <span class="token function">updateItemCallback</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> updatedItem<span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> updatedItems<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">appReducer</span><span class="token punctuation">(</span>state <span class="token operator">=</span> initialState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">switch</span><span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token string">&apos;SET_VISIBILITY_FILTER&apos;</span> <span class="token punctuation">:</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> <span class="token punctuation">{</span>visibilityFilter <span class="token punctuation">:</span> action<span class="token punctuation">.</span>filter<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">case</span> <span class="token string">&apos;ADD_TODO&apos;</span> <span class="token punctuation">:</span> <span class="token punctuation">{</span>
            <span class="token keyword">const</span> newTodos <span class="token operator">=</span> state<span class="token punctuation">.</span>todos<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
                id<span class="token punctuation">:</span> action<span class="token punctuation">.</span>id<span class="token punctuation">,</span>
                text<span class="token punctuation">:</span> action<span class="token punctuation">.</span>text<span class="token punctuation">,</span>
                completed<span class="token punctuation">:</span> <span class="token keyword">false</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> <span class="token punctuation">{</span>todos <span class="token punctuation">:</span> newTodos<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">case</span> <span class="token string">&apos;TOGGLE_TODO&apos;</span> <span class="token punctuation">:</span> <span class="token punctuation">{</span>
            <span class="token keyword">const</span> newTodos <span class="token operator">=</span> <span class="token function">updateItemInArray</span><span class="token punctuation">(</span>state<span class="token punctuation">.</span>todos<span class="token punctuation">,</span> action<span class="token punctuation">.</span>id<span class="token punctuation">,</span> todo <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>todo<span class="token punctuation">,</span> <span class="token punctuation">{</span>completed <span class="token punctuation">:</span> <span class="token operator">!</span>todo<span class="token punctuation">.</span>completed<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> <span class="token punctuation">{</span>todos <span class="token punctuation">:</span> newTodos<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">case</span> <span class="token string">&apos;EDIT_TODO&apos;</span> <span class="token punctuation">:</span> <span class="token punctuation">{</span>
            <span class="token keyword">const</span> newTodos <span class="token operator">=</span> <span class="token function">updateItemInArray</span><span class="token punctuation">(</span>state<span class="token punctuation">.</span>todos<span class="token punctuation">,</span> action<span class="token punctuation">.</span>id<span class="token punctuation">,</span> todo <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>todo<span class="token punctuation">,</span> <span class="token punctuation">{</span>text <span class="token punctuation">:</span> action<span class="token punctuation">.</span>text<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> <span class="token punctuation">{</span>todos <span class="token punctuation">:</span> newTodos<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">default</span> <span class="token punctuation">:</span> <span class="token keyword">return</span> state<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x6837;&#x5C31;&#x51CF;&#x5C11;&#x4E86;&#x91CD;&#x590D;&#xFF0C;&#x4F7F;&#x5F97;&#x4EE3;&#x7801;&#x7684;&#x53EF;&#x8BFB;&#x6027;&#x66F4;&#x9AD8;&#x3002;</p>
<h4 id="%E6%8F%90%E5%8F%96-case-reducer">&#x63D0;&#x53D6; case reducer</h4>
<p>&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x628A;&#x7279;&#x6B8A;&#x903B;&#x8F91;&#x5C01;&#x88C5;&#x6210;&#x5BF9;&#x5E94;&#x7684;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="token comment" spellcheck="true">// &#x7701;&#x7565;&#x4E86;&#x5185;&#x5BB9;</span>
<span class="token keyword">function</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>oldObject<span class="token punctuation">,</span> newValues<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">updateItemInArray</span><span class="token punctuation">(</span>array<span class="token punctuation">,</span> itemId<span class="token punctuation">,</span> updateItemCallback<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>


<span class="token keyword">function</span> <span class="token function">setVisibilityFilter</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> <span class="token punctuation">{</span>visibilityFilter <span class="token punctuation">:</span> action<span class="token punctuation">.</span>filter <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">addTodo</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> newTodos <span class="token operator">=</span> state<span class="token punctuation">.</span>todos<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
        id<span class="token punctuation">:</span> action<span class="token punctuation">.</span>id<span class="token punctuation">,</span>
        text<span class="token punctuation">:</span> action<span class="token punctuation">.</span>text<span class="token punctuation">,</span>
        completed<span class="token punctuation">:</span> <span class="token keyword">false</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> <span class="token punctuation">{</span>todos <span class="token punctuation">:</span> newTodos<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">toggleTodo</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> newTodos <span class="token operator">=</span> <span class="token function">updateItemInArray</span><span class="token punctuation">(</span>state<span class="token punctuation">.</span>todos<span class="token punctuation">,</span> action<span class="token punctuation">.</span>id<span class="token punctuation">,</span> todo <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>todo<span class="token punctuation">,</span> <span class="token punctuation">{</span>completed <span class="token punctuation">:</span> <span class="token operator">!</span>todo<span class="token punctuation">.</span>completed<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> <span class="token punctuation">{</span>todos <span class="token punctuation">:</span> newTodos<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">editTodo</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> newTodos <span class="token operator">=</span> <span class="token function">updateItemInArray</span><span class="token punctuation">(</span>state<span class="token punctuation">.</span>todos<span class="token punctuation">,</span> action<span class="token punctuation">.</span>id<span class="token punctuation">,</span> todo <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>todo<span class="token punctuation">,</span> <span class="token punctuation">{</span>text <span class="token punctuation">:</span> action<span class="token punctuation">.</span>text<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> <span class="token punctuation">{</span>todos <span class="token punctuation">:</span> newTodos<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">appReducer</span><span class="token punctuation">(</span>state <span class="token operator">=</span> initialState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">switch</span><span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token string">&apos;SET_VISIBILITY_FILTER&apos;</span> <span class="token punctuation">:</span> <span class="token keyword">return</span> <span class="token function">setVisibilityFilter</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> action<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&apos;ADD_TODO&apos;</span> <span class="token punctuation">:</span> <span class="token keyword">return</span> <span class="token function">addTodo</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> action<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&apos;TOGGLE_TODO&apos;</span> <span class="token punctuation">:</span> <span class="token keyword">return</span> <span class="token function">toggleTodo</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> action<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&apos;EDIT_TODO&apos;</span> <span class="token punctuation">:</span> <span class="token keyword">return</span> <span class="token function">editTodo</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> action<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">default</span> <span class="token punctuation">:</span> <span class="token keyword">return</span> state<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x5F88;&#x6E05;&#x695A;&#x6BCF;&#x4E2A; <code><span class="token keyword">case</span></code> &#x53D1;&#x751F;&#x4E86;&#x4EC0;&#x4E48;&#x3002;&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x4E00;&#x4E9B;&#x6A21;&#x5F0F;&#x7684;&#x96CF;&#x5F62;&#x3002;</p>
<h4 id="%E6%8C%89%E5%9F%9F%E6%8B%86%E5%88%86%E6%95%B0%E6%8D%AE%EF%BC%88separating-data-handling-by-domain%EF%BC%89">&#x6309;&#x57DF;&#x62C6;&#x5206;&#x6570;&#x636E;&#xFF08;Separating Data Handling by Domain&#xFF09;</h4>
<p>&#x76EE;&#x524D;&#x7684; Reducer &#x4ECD;&#x7136;&#x9700;&#x8981;&#x5173;&#x5FC3;&#x7A0B;&#x5E8F;&#x4E2D;&#x6240;&#x6709;&#x4E0D;&#x540C;&#x7684; case&#x3002;&#x4E0B;&#x9762;&#x5C1D;&#x8BD5;&#x628A; filter &#x903B;&#x8F91;&#x548C; todo &#x903B;&#x8F91;&#x5206;&#x79BB;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="token comment" spellcheck="true">// &#x7701;&#x7565;&#x4E86;&#x5185;&#x5BB9;</span>
<span class="token keyword">function</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>oldObject<span class="token punctuation">,</span> newValues<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">updateItemInArray</span><span class="token punctuation">(</span>array<span class="token punctuation">,</span> itemId<span class="token punctuation">,</span> updateItemCallback<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>



<span class="token keyword">function</span> <span class="token function">setVisibilityFilter</span><span class="token punctuation">(</span>visibilityState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">// &#x4ECE;&#x6280;&#x672F;&#x4E0A;&#x5C06;&#xFF0C;&#x6211;&#x4EEC;&#x751A;&#x81F3;&#x4E0D;&#x5173;&#x5FC3;&#x4E4B;&#x524D;&#x7684;&#x72B6;&#x6001;</span>
    <span class="token keyword">return</span> action<span class="token punctuation">.</span>filter<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">visibilityReducer</span><span class="token punctuation">(</span>visibilityState <span class="token operator">=</span> <span class="token string">&apos;SHOW_ALL&apos;</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">switch</span><span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token string">&apos;SET_VISIBILITY_FILTER&apos;</span> <span class="token punctuation">:</span> <span class="token keyword">return</span> <span class="token function">setVisibilityFilter</span><span class="token punctuation">(</span>visibilityState<span class="token punctuation">,</span> action<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">default</span> <span class="token punctuation">:</span> <span class="token keyword">return</span> visibilityState<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>


<span class="token keyword">function</span> <span class="token function">addTodo</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> newTodos <span class="token operator">=</span> todosState<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
        id<span class="token punctuation">:</span> action<span class="token punctuation">.</span>id<span class="token punctuation">,</span>
        text<span class="token punctuation">:</span> action<span class="token punctuation">.</span>text<span class="token punctuation">,</span>
        completed<span class="token punctuation">:</span> <span class="token keyword">false</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> newTodos<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">toggleTodo</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> newTodos <span class="token operator">=</span> <span class="token function">updateItemInArray</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">.</span>id<span class="token punctuation">,</span> todo <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>todo<span class="token punctuation">,</span> <span class="token punctuation">{</span>completed <span class="token punctuation">:</span> <span class="token operator">!</span>todo<span class="token punctuation">.</span>completed<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> newTodos<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">editTodo</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> newTodos <span class="token operator">=</span> <span class="token function">updateItemInArray</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">.</span>id<span class="token punctuation">,</span> todo <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>todo<span class="token punctuation">,</span> <span class="token punctuation">{</span>text <span class="token punctuation">:</span> action<span class="token punctuation">.</span>text<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> newTodos<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">todosReducer</span><span class="token punctuation">(</span>todosState <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">switch</span><span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token string">&apos;ADD_TODO&apos;</span> <span class="token punctuation">:</span> <span class="token keyword">return</span> <span class="token function">addTodo</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&apos;TOGGLE_TODO&apos;</span> <span class="token punctuation">:</span> <span class="token keyword">return</span> <span class="token function">toggleTodo</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&apos;EDIT_TODO&apos;</span> <span class="token punctuation">:</span> <span class="token keyword">return</span> <span class="token function">editTodo</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">default</span> <span class="token punctuation">:</span> <span class="token keyword">return</span> todosState<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">appReducer</span><span class="token punctuation">(</span>state <span class="token operator">=</span> initialState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>
        todos <span class="token punctuation">:</span> <span class="token function">todosReducer</span><span class="token punctuation">(</span>state<span class="token punctuation">.</span>todos<span class="token punctuation">,</span> action<span class="token punctuation">)</span><span class="token punctuation">,</span>
        visibilityFilter <span class="token punctuation">:</span> <span class="token function">visibilityReducer</span><span class="token punctuation">(</span>state<span class="token punctuation">.</span>visibilityFilter<span class="token punctuation">,</span> action<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x6CE8;&#x610F;&#x5230;&#xFF0C;&#x4E24;&#x4E2A; reducer &#x5206;&#x522B;&#x5173;&#x5FC3; state &#x4E2D;&#x7684;&#x4E0D;&#x540C;&#x7684;&#x90E8;&#x5206;&#x3002;&#x90FD;&#x53EA;&#x9700;&#x8981;&#x628A;&#x81EA;&#x8EAB;&#x5173;&#x5FC3;&#x7684;&#x6570;&#x636E;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x4E0D;&#x518D;&#x9700;&#x8981;&#x8FD4;&#x56DE;&#x590D;&#x6742;&#x7684;&#x5D4C;&#x5957;&#x578B; state &#x5BF9;&#x8C61;&#x4E86;&#xFF0C;&#x4EE3;&#x7801;&#x53D8;&#x5F97;&#x66F4;&#x7B80;&#x5355;&#x3002;</p>
<h4 id="%E5%87%8F%E5%B0%91%E6%A0%B7%E6%9D%BF%E4%BB%A3%E7%A0%81">&#x51CF;&#x5C11;&#x6837;&#x677F;&#x4EE3;&#x7801;</h4>
<p>&#x9A6C;&#x4E0A;&#x5C31;&#x5927;&#x529F;&#x544A;&#x6210;&#x4E86;&#x3002;&#x56E0;&#x4E3A;&#x5F88;&#x591A;&#x4EBA;&#x4E0D;&#x559C;&#x6B22;&#x4F7F;&#x7528; switch &#x8FD9;&#x79CD;&#x8BED;&#x6CD5;&#x7ED3;&#x6784;&#xFF0C;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; action &#x5230; case &#x67E5;&#x627E;&#x8868;&#x793A;&#x975E;&#x5E38;&#x901A;&#x7528;&#x7684;&#x505A;&#x6CD5;&#x3002;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <a href="ReducingBoilerplate.md#generating-reducers">&#x7F29;&#x51CF;&#x6837;&#x677F;&#x4EE3;&#x7801;</a> &#x4E2D;&#x63D0;&#x5230;&#x7684; <code>createReducer</code> &#x51FD;&#x6570;&#x51CF;&#x5C11;&#x6837;&#x677F;&#x4EE3;&#x7801;&#x3002;</p>
<pre><code class="lang-javascript"><span class="token comment" spellcheck="true">// &#x7701;&#x7565;&#x4E86;&#x5185;&#x5BB9;</span>
<span class="token keyword">function</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>oldObject<span class="token punctuation">,</span> newValues<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">updateItemInArray</span><span class="token punctuation">(</span>array<span class="token punctuation">,</span> itemId<span class="token punctuation">,</span> updateItemCallback<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">createReducer</span><span class="token punctuation">(</span>initialState<span class="token punctuation">,</span> handlers<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token function">reducer</span><span class="token punctuation">(</span>state <span class="token operator">=</span> initialState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>handlers<span class="token punctuation">.</span><span class="token function">hasOwnProperty</span><span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> handlers<span class="token punctuation">[</span>action<span class="token punctuation">.</span>type<span class="token punctuation">]</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> action<span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> state
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>


<span class="token comment" spellcheck="true">// &#x7701;&#x7565;&#x4E86;&#x5185;&#x5BB9;</span>
<span class="token keyword">function</span> <span class="token function">setVisibilityFilter</span><span class="token punctuation">(</span>visibilityState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token keyword">const</span> visibilityReducer <span class="token operator">=</span> <span class="token function">createReducer</span><span class="token punctuation">(</span><span class="token string">&apos;SHOW_ALL&apos;</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    <span class="token string">&apos;SET_VISIBILITY_FILTER&apos;</span> <span class="token punctuation">:</span> setVisibilityFilter
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// &#x7701;&#x7565;&#x4E86;&#x5185;&#x5BB9;</span>
<span class="token keyword">function</span> <span class="token function">addTodo</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">toggleTodo</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">editTodo</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token keyword">const</span> todosReducer <span class="token operator">=</span> <span class="token function">createReducer</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    <span class="token string">&apos;ADD_TODO&apos;</span> <span class="token punctuation">:</span> addTodo<span class="token punctuation">,</span>
    <span class="token string">&apos;TOGGLE_TODO&apos;</span> <span class="token punctuation">:</span> toggleTodo<span class="token punctuation">,</span>
    <span class="token string">&apos;EDIT_TODO&apos;</span> <span class="token punctuation">:</span> editTodo
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">appReducer</span><span class="token punctuation">(</span>state <span class="token operator">=</span> initialState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>
        todos <span class="token punctuation">:</span> <span class="token function">todosReducer</span><span class="token punctuation">(</span>state<span class="token punctuation">.</span>todos<span class="token punctuation">,</span> action<span class="token punctuation">)</span><span class="token punctuation">,</span>
        visibilityFilter <span class="token punctuation">:</span> <span class="token function">visibilityReducer</span><span class="token punctuation">(</span>state<span class="token punctuation">.</span>visibilityFilter<span class="token punctuation">,</span> action<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h4 id="%E9%80%9A%E8%BF%87%E5%88%87%E7%89%87%E7%BB%84%E5%90%88-reducer%EF%BC%88combining-reducers-by-slice%EF%BC%89">&#x901A;&#x8FC7;&#x5207;&#x7247;&#x7EC4;&#x5408; Reducer&#xFF08;Combining Reducers by Slice&#xFF09;</h4>
<p>&#x6700;&#x540E;&#x4E00;&#x6B65;&#x4E86;&#xFF0C;&#x4F7F;&#x7528; Redux &#x4E2D; <code>combineReducers</code> &#x8FD9;&#x4E2A;&#x5DE5;&#x5177;&#x51FD;&#x6570;&#x53BB;&#x628A;&#x7BA1;&#x7406;&#x6BCF;&#x4E2A; state &#x5207;&#x7247;&#x7684;&#x903B;&#x8F91;&#x7EC4;&#x5408;&#x8D77;&#x6765;&#xFF0C;&#x5F62;&#x6210;&#x9876;&#x5C42;&#x7684; reducer&#x3002;&#x6700;&#x7EC8;&#x53D8;&#x6210;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="token comment" spellcheck="true">// &#x53EF;&#x91CD;&#x7528;&#x7684;&#x5DE5;&#x5177;&#x51FD;&#x6570;</span>

<span class="token keyword">function</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>oldObject<span class="token punctuation">,</span> newValues<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">// &#x5C06;&#x7A7A;&#x5BF9;&#x8C61;&#x4F5C;&#x4E3A;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9; Object.assign&#xFF0C;&#x4EE5;&#x786E;&#x4FDD;&#x53EA;&#x662F;&#x590D;&#x5236;&#x6570;&#x636E;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x53BB;&#x6539;&#x53D8;&#x6570;&#x636E;</span>
    <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> oldObject<span class="token punctuation">,</span> newValues<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">updateItemInArray</span><span class="token punctuation">(</span>array<span class="token punctuation">,</span> itemId<span class="token punctuation">,</span> updateItemCallback<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> updatedItems <span class="token operator">=</span> array<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span>item <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>id <span class="token operator">!</span><span class="token operator">==</span> itemId<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">// &#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x53EA;&#x60F3;&#x66F4;&#x65B0;&#x4E00;&#x4E2A;&#x9879;&#x76EE;&#xFF0C;&#x6240;&#x4EE5;&#x4FDD;&#x7559;&#x6240;&#x6709;&#x7684;&#x5176;&#x4ED6;&#x9879;&#x76EE;</span>
            <span class="token keyword">return</span> item<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

         <span class="token comment" spellcheck="true">// &#x4F7F;&#x7528;&#x63D0;&#x4F9B;&#x7684;&#x56DE;&#x8C03;&#x6765;&#x521B;&#x5EFA;&#x65B0;&#x7684;&#x9879;&#x76EE;</span>
        <span class="token keyword">const</span> updatedItem <span class="token operator">=</span> <span class="token function">updateItemCallback</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> updatedItem<span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> updatedItems<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">createReducer</span><span class="token punctuation">(</span>initialState<span class="token punctuation">,</span> handlers<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token function">reducer</span><span class="token punctuation">(</span>state <span class="token operator">=</span> initialState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>handlers<span class="token punctuation">.</span><span class="token function">hasOwnProperty</span><span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> handlers<span class="token punctuation">[</span>action<span class="token punctuation">.</span>type<span class="token punctuation">]</span><span class="token punctuation">(</span>state<span class="token punctuation">,</span> action<span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> state
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>


<span class="token comment" spellcheck="true">// &#x5904;&#x7406;&#x7279;&#x6B8A; case &#x7684; Handler (&quot;case reducer&quot;)</span>
<span class="token keyword">function</span> <span class="token function">setVisibilityFilter</span><span class="token punctuation">(</span>visibilityState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">// &#x4ECE;&#x6280;&#x672F;&#x4E0A;&#x5C06;&#xFF0C;&#x6211;&#x4EEC;&#x751A;&#x81F3;&#x4E0D;&#x5173;&#x5FC3;&#x4E4B;&#x524D;&#x7684;&#x72B6;&#x6001;</span>
    <span class="token keyword">return</span> action<span class="token punctuation">.</span>filter<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// &#x5904;&#x7406;&#x6574;&#x4E2A; state &#x5207;&#x7247;&#x7684; Handler (&quot;slice reducer&quot;)</span>
<span class="token keyword">const</span> visibilityReducer <span class="token operator">=</span> <span class="token function">createReducer</span><span class="token punctuation">(</span><span class="token string">&apos;SHOW_ALL&apos;</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    <span class="token string">&apos;SET_VISIBILITY_FILTER&apos;</span> <span class="token punctuation">:</span> setVisibilityFilter
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// Case reducer</span>
<span class="token keyword">function</span> <span class="token function">addTodo</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> newTodos <span class="token operator">=</span> todosState<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
        id<span class="token punctuation">:</span> action<span class="token punctuation">.</span>id<span class="token punctuation">,</span>
        text<span class="token punctuation">:</span> action<span class="token punctuation">.</span>text<span class="token punctuation">,</span>
        completed<span class="token punctuation">:</span> <span class="token keyword">false</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> newTodos<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// Case reducer</span>
<span class="token keyword">function</span> <span class="token function">toggleTodo</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> newTodos <span class="token operator">=</span> <span class="token function">updateItemInArray</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">.</span>id<span class="token punctuation">,</span> todo <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>todo<span class="token punctuation">,</span> <span class="token punctuation">{</span>completed <span class="token punctuation">:</span> <span class="token operator">!</span>todo<span class="token punctuation">.</span>completed<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> newTodos<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// Case reducer</span>
<span class="token keyword">function</span> <span class="token function">editTodo</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> newTodos <span class="token operator">=</span> <span class="token function">updateItemInArray</span><span class="token punctuation">(</span>todosState<span class="token punctuation">,</span> action<span class="token punctuation">.</span>id<span class="token punctuation">,</span> todo <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token function">updateObject</span><span class="token punctuation">(</span>todo<span class="token punctuation">,</span> <span class="token punctuation">{</span>text <span class="token punctuation">:</span> action<span class="token punctuation">.</span>text<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> newTodos<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// Slice reducer</span>
<span class="token keyword">const</span> todosReducer <span class="token operator">=</span> <span class="token function">createReducer</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    <span class="token string">&apos;ADD_TODO&apos;</span> <span class="token punctuation">:</span> addTodo<span class="token punctuation">,</span>
    <span class="token string">&apos;TOGGLE_TODO&apos;</span> <span class="token punctuation">:</span> toggleTodo<span class="token punctuation">,</span>
    <span class="token string">&apos;EDIT_TODO&apos;</span> <span class="token punctuation">:</span> editTodo
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// &#x9876;&#x5C42; reducer</span>
<span class="token keyword">const</span> appReducer <span class="token operator">=</span> <span class="token function">combineReducers</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    visibilityFilter <span class="token punctuation">:</span> visibilityReducer<span class="token punctuation">,</span>
    todos <span class="token punctuation">:</span> todosReducer
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x6709;&#x4E86;&#x5206;&#x79BB;&#x96C6;&#x4E2D; reducer &#x7684;&#x4F8B;&#x5B50;&#xFF1A;&#x50CF; <code>updateObject</code> &#x548C; <code>createReducer</code> &#x4E00;&#x6837;&#x7684;&#x5DE5;&#x5177;&#x51FD;&#x6570;&#xFF0C;&#x50CF; <code>setVisibilityFilter</code> &#x548C; <code>addTodo</code> &#x4E00;&#x6837;&#x7684;&#x5904;&#x7406;&#x5668;&#xFF08;Handler&#xFF09;&#xFF0C;&#x50CF; <code>visibilityReducer</code> &#x548C; <code>todosReducer</code> &#x4E00;&#x6837;&#x7684;&#x5904;&#x7406;&#x5355;&#x4E2A;&#x5207;&#x7247;&#x6570;&#x636E;&#x7684; Handler&#x3002;<code>appReducer</code> &#x53EF;&#x4EE5;&#x88AB;&#x5F53;&#x4F5C;&#x662F;&#x9876;&#x5C42; reducer&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#x6700;&#x540E;&#x7684;&#x7ED3;&#x679C;&#x770B;&#x4E0A;&#x53BB;&#x6BD4;&#x539F;&#x59CB;&#x7684;&#x7248;&#x672C;&#x66F4;&#x957F;&#xFF0C;&#x8FD9;&#x4E3B;&#x8981;&#x662F;&#x56E0;&#x4E3A;&#x5DE5;&#x5177;&#x51FD;&#x6570;&#x7684;&#x63D0;&#x53D6;&#xFF0C;&#x6CE8;&#x91CA;&#x7684;&#x6DFB;&#x52A0;&#x548C;&#x4E00;&#x4E9B;&#x4E3A;&#x4E86;&#x6E05;&#x695A;&#x8D77;&#x89C1;&#x7684;&#x6545;&#x610F;&#x5197;&#x957F;&#xFF08;&#x6BD4;&#x5982;&#x5355;&#x72EC;&#x7684; return &#x8BED;&#x53E5;&#xFF09;&#x3002;&#x5355;&#x72EC;&#x7684;&#x770B;&#x6BCF;&#x4E2A;&#x529F;&#x80FD;&#xFF0C;&#x4ED6;&#x4EEC;&#x627F;&#x62C5;&#x7684;&#x8D23;&#x4EFB;&#x66F4;&#x5C0F;&#xFF0C;&#x610F;&#x56FE;&#x4E5F;&#x66F4;&#x52A0;&#x6E05;&#x695A;&#x3002;&#x5728;&#x771F;&#x6B63;&#x7684;&#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x8FD9;&#x4E9B;&#x51FD;&#x6570;&#x5C06;&#x4F1A;&#x5206;&#x5230;&#x5355;&#x72EC;&#x7684;&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x6BD4;&#x5982;&#xFF1A;<code>reducerUtilities<span class="token punctuation">.</span>js</code>&#xFF0C;<code>visibilityReducer<span class="token punctuation">.</span>js</code>&#xFF0C;<code>todosReudcer<span class="token punctuation">.</span>js</code> &#x548C; <code>rootReducer<span class="token punctuation">.</span>js</code>&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../../docs/recipes/reducers/SplittingReducerLogic.html" class="navigation navigation-prev " aria-label="Previous page: Reducer 逻辑拆分"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../../docs/recipes/reducers/UsingCombineReducers.html" class="navigation navigation-next " aria-label="Next page: `combineReducers` 用法"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../../gitbook/app.js"></script>

    
    <script src="../../../gitbook/plugins/gitbook-plugin-edit-link/plugin.js"></script>
    

    
    <script src="../../../gitbook/plugins/gitbook-plugin-ga/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2},"edit-link":{"base":"https://github.com/camsong/redux-in-chinese/tree/master","label":"开始纠错"},"ga":{"token":"UA-66122997-1","configuration":"auto"}};
    gitbook.start(config);
});
</script>

        <script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-66122997-1', 'auto');ga('send', 'pageview');</script>
    </body>
    
</html>
